﻿using System;
using System.Net;
using System.Windows;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Xml.Serialization;

namespace Acando.Dynamics.CRM.Silverlight.Library.MetaSdk
{
    public partial class EntityMetadata
    {
        
        private Dictionary<string, AttributeMetadata> _cache;

        public Dictionary<string, AttributeMetadata> AttributesDict()
        {
            if (this._cache == null)
                this._cache = this.attributesField.ToDictionary(c => c.LogicalName, c => c);
                
            return this._cache;
        }

        public CrmAttributeType GetAttributeType(string attributename)
        {
            return this.GetAttributeMetadata(attributename).AttributeType;
        }

        public Dictionary<string, AttributeMetadata> GetAttributesDict()
        {
            if (this._cache == null)
                this._cache = this.attributesField.ToDictionary(c => c.LogicalName, c => c);
           
            return _cache;
           
        }

        public AttributeMetadata GetAttributeMetadata(string attributename)
        {
            var dict = this.AttributesDict();
            
            if (dict.Keys.Contains(attributename))
                return dict[attributename];
            else
                throw new Exception("Unknown attribute: " + attributename);
        }

    }
}
